[[expressions-operator-ternary]]
= Ternary Operator (If-Then-Else)

You can use the ternary operator for performing if-then-else conditional logic inside
the expression. The following listing shows a minimal example:

[tabs]
======
Java::
+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
----
	String falseString = parser.parseExpression(
			"false ? 'trueExp' : 'falseExp'").getValue(String.class);
----

Kotlin::
+
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
----
	val falseString = parser.parseExpression(
			"false ? 'trueExp' : 'falseExp'").getValue(String::class.java)
----
======

In this case, the boolean `false` results in returning the string value `'falseExp'`. A more
realistic example follows:

[tabs]
======
Java::
+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
----
	parser.parseExpression("name").setValue(societyContext, "IEEE");
	societyContext.setVariable("queryName", "Nikola Tesla");

	expression = "isMember(#queryName)? #queryName + ' is a member of the ' " +
			"+ Name + ' Society' : #queryName + ' is not a member of the ' + Name + ' Society'";

	String queryResultString = parser.parseExpression(expression)
			.getValue(societyContext, String.class);
	// queryResultString = "Nikola Tesla is a member of the IEEE Society"
----

Kotlin::
+
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
----
	parser.parseExpression("name").setValue(societyContext, "IEEE")
	societyContext.setVariable("queryName", "Nikola Tesla")

	expression = "isMember(#queryName)? #queryName + ' is a member of the ' " + "+ Name + ' Society' : #queryName + ' is not a member of the ' + Name + ' Society'"

	val queryResultString = parser.parseExpression(expression)
			.getValue(societyContext, String::class.java)
	// queryResultString = "Nikola Tesla is a member of the IEEE Society"
----
======

See the next section on the Elvis operator for an even shorter syntax for the
ternary operator.



